/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdaboostM1SettingsPanel.java
 *
 * Created on Dec 11, 2009, 1:33:23 PM
 */

package wekinator.LearningAlgorithms;

import javax.swing.JPanel;
import weka.classifiers.meta.AdaBoostM1;
import weka.classifiers.trees.DecisionStump;
import weka.classifiers.trees.J48;

/**
 *
 * @author rebecca
 */
public class AdaboostM1SettingsPanel extends javax.swing.JPanel implements LearningAlgorithmSettingsPanel {

    AdaboostM1LearningAlgorithm la = null;

    /** Creates new form AdaboostM1SettingsPanel */
    public AdaboostM1SettingsPanel() {
        initComponents();
    }

    public AdaboostM1SettingsPanel(AdaboostM1LearningAlgorithm la) {
        initComponents();
        setLearningAlgorithm(la);
    }

    public void setLearningAlgorithm(AdaboostM1LearningAlgorithm la) {
        this.la = la;
        textNumRounds.setText(la.getClassifier().getNumIterations() + ""); //TODO: Might classifier be null?
        int index = la.isBaseTree ? 0 : 1;
        comboBaseClassifier.setSelectedIndex(index);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textNumRounds = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        comboBaseClassifier = new javax.swing.JComboBox();

        textNumRounds.setText("100");

        jLabel1.setText("Number of training rounds");

        jLabel2.setText("Base classifier");

        comboBaseClassifier.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Decision Tree", "Decision Stump" }));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 34, Short.MAX_VALUE)
                        .add(textNumRounds, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(comboBaseClassifier, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(textNumRounds, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(comboBaseClassifier, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void applySettings() throws Exception {
        boolean valid = validateSettings();
        if (valid) {
            int i = Integer.parseInt(textNumRounds.getText());
            la.getClassifier().setNumIterations(i);
            if (comboBaseClassifier.getSelectedIndex() == 0) {
                la.getClassifier().setClassifier(new J48());
            } else {
                la.getClassifier().setClassifier(new DecisionStump());
            }
        } else {
            throw new Exception("Invalid settings");
        }
    }

    public boolean validateSettings() {
        try {
            int i = Integer.parseInt(textNumRounds.getText());
            if (i > 0)
                return true;
        } catch (Exception ex) {
            return false;
        }
        return false;
    }

    public void reset() {
        textNumRounds.setText(la.getClassifier().getNumIterations()+ "");
    }

    public JPanel getPanel() {
        return this;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboBaseClassifier;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField textNumRounds;
    // End of variables declaration//GEN-END:variables

}
